﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.DTO.Infos;

namespace Demo.Services.Integration.DAO.IntegrationComponents.Mappings
{
    public class OperationDAOMapper
    {

        /// <summary>
        /// Obtiene una lista de operaciones
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static OperationPagingInfo GetOperationList(DataSet ds)
        {
            List<OperationInfo> operations;
            OperationPagingInfo result;
            try
            {
                Logger.Info();
                DataTable dt = ds.Tables[0];
                result = new OperationPagingInfo();
                result.Pagination = new PaginationInfo();
                result.Pagination.TotalCount = (int)dt.Rows[0]["ToltalReg"];


                dt = ds.Tables[1];
                operations = (from operation in dt.AsEnumerable()
                              select new OperationInfo
                              {
                                  OperationId = operation.Field<int>("id"),
                                  Proyect = operation.Field<string>("proyecto"),
                                  DateStart = operation.Field<DateTime>("fechaInicio").ToString("dd-MM-yyyy"),
                                  DateEnd = operation.Field<DateTime>("fechaFin").ToString("dd-MM-yyyy"),
                                  Hour = operation.Field<int>("horas"),
                                  HourAssigned = operation.Field<int>("horasAsignadas")
                              }
                    ).ToList();

                
                result.Operations = operations;

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }


        /// <summary>
        /// Obtiene una lista de recursos asignados a una operacion
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static ResourcePagingInfo GetOperationResourcesList(DataSet ds)
        {
            List<ResourceInfo> resources;
            ResourcePagingInfo result;
            try
            {
                Logger.Info();
                DataTable dt = ds.Tables[0];
                result = new ResourcePagingInfo();
                
                result.TotalReg = (int)dt.Rows[0]["ToltalReg"];


                dt = ds.Tables[1];
                resources = (from resource in dt.AsEnumerable()
                             select new ResourceInfo
                              {
                                  Id = resource.Field<int>("id"),
                                  Name = resource.Field<string>("nombre"),
                                  TypeLevel = resource.Field<string>("tiponivel"),
                                  Cost = resource.Field<decimal>("costo"),
                                  DateStart = resource.Field<DateTime>("fechaInicio").ToString("dd-MM-yyyy"),
                                  DateEnd = resource.Field<DateTime>("fechaFinal").ToString("dd-MM-yyyy"),
                                  HourAssigned = resource.Field<int>("horasAsignadas")
                              }
                    ).ToList();


                result.Resources = resources;

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }

        /// <summary>
        /// Obtiene una lista de recursos asignados a una operacion
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static ResourceInfo GetResourceLineTime(DataSet ds)
        {
            ResourceInfo result;
            try
            {
                Logger.Info();
                DataTable dt = ds.Tables[0];
                result = (from resource in dt.AsEnumerable()
                             select new ResourceInfo
                             {
                                 Id = resource.Field<int>("id"),
                                 Name = resource.Field<string>("nombre"),
                                 TypeLevel = resource.Field<string>("tipoRecurso"),
                                 Cost = resource.Field<decimal>("costo"),
                                 DateStart = resource.Field<DateTime>("fechaInicio").ToString("dd-MM-yyyy"),
                                 DateEnd = resource.Field<DateTime>("fechaFinal").ToString("dd-MM-yyyy")                                 
                             }
                    ).First<ResourceInfo>();
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }

    }
}
